package com.thinqq.tracktask.client;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.thinqq.tracktask.client.TrackTaskRequestEngine.Observer;
import com.thinqq.tracktask.shared.FieldVerifier;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Tracktask implements EntryPoint, Observer {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
		+ "attempting to contact the server. Please check your network "
		+ "connection and try again.";

	private VerticalPanel dialogVPanel;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// first widget
		final Button sendButton = new Button("Add Task");
		final TextBox nameField = new TextBox();
		nameField.setText("help on coding");
		nameField.setWidth("98%");

		final Label errorLabel = new Label();
		sendButton.addStyleName("sendButton");

		// second widget
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Task List");
		dialogBox.setAnimationEnabled(true);
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		serverResponseLabel.setStyleName("test");
		dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");

		dialogVPanel.add(new Label("sample task name"));
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogBox.setWidget(dialogVPanel);

		// Add those into root
		RootPanel.get("nameFieldContainer").add(nameField);
		RootPanel.get("sendButtonContainer").add(sendButton);
		RootPanel.get("errorLabelContainer").add(errorLabel);
		RootPanel.get("tasklistContainer").add(dialogBox);

		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);
		nameField.selectAll();

		// on module load get task list and update the ui
		final TrackTaskRequestEngine tte = TrackTaskRequestEngine.getInstance();
		tte.addTaskListObserver(this);
		tte.executeGetTaskList();

		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				executeAddTask();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					executeAddTask();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a
			 * response.
			 */
			private void executeAddTask() {
				// First, we validate the input.
				errorLabel.setText("");
				String textToServer = nameField.getText();
				if (!FieldVerifier.isValidName(textToServer)) {
					errorLabel.setText("Please enter at least four characters");
					return;
				}

				TrackTaskRequestEngine.getInstance().executeAddTask(textToServer);
			}
		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		nameField.addKeyUpHandler(handler);
	}

	@Override
	public void executeDisplay(Object... obj) {
		dialogVPanel.clear();
		for (String string : (List<String>) obj[0]) {
			dialogVPanel.add(new Label(string));
		}
	}
}
